const fly=require("flyio")
module.exports = class extends think.cmswing.rest {
	
   /**
   * 获取分类信息
   * /api/shop 获取全部代理
   * /api/shop/1 获取代理id为1的栏目信息
   * @returns {Promise.<*>}
   */
	
  async getAction() {
   const cmd = this.get('cmd') || '';
   const roomno = this.get('roomno') || '';
   const uid = this.get('uid') || '';
   
   if (think.isEmpty(cmd)){
	   return this.fail('命令不能为空！');
   } 
   
   if(cmd!=='boxlist'&&think.isEmpty(roomno)) {
       return this.fail('房间号不能为空！');
   }
   
   if (think.isEmpty(uid)) {
       return this.fail('玩家ID不能为空！');
   } 
   
   var dd = new Date();
   var fin_time = dd.valueOf()+(Number(this.config('setup.SAFEBOX_DEADLINE')) * 60000);
   fly.config.timeout=10000;
   fly.config.parseJson=true;
   fly.interceptors.response.use(
		    (response) => {
		        //只将请求结果的data字段返回
		        return response.data
		    },
		    (err) => {
		        //发生网络错误后会走到这里
		        //return Promise.resolve("ssss")
		    }
    )
   
   if(cmd=='verroomno'){
	   const sbdata = await this.model('safebox').where({password:roomno,uid:uid}).find();
	   console.log("sbdata ---------------:"+JSON.stringify(sbdata));
	   if (think.isEmpty(sbdata)) {
		   return this.fail('房间号不存在！');
	   }else{
		  //3:操作失败;2:操作成功;1:待处理;0：待存入/待取出;-1:超时失败;
		  if(sbdata.status==-1){
             return this.fail('房间号已失效！');
		  }else if((sbdata.status==2||sbdata.status==3)&&dd.valueOf()>sbdata.fin_time){
             return this.fail('房间号已失效！');
		  }else if((sbdata.status==0||sbdata.status==1)&&dd.valueOf()>sbdata.exp_time){
             return this.fail('房间号已失效！');
		  }else{
			  const resr = {};
			  resr.roomno = sbdata.password;
			  resr.type = sbdata.catalog=='1'?1:2;
			  resr.coin = (sbdata.status==0||sbdata.status==1)?sbdata.deposit_count:0;
			  return this.success(resr);
		  }
	   }
	   
   }else if(cmd=='depcoin'){
	   const coin = this.get('coin') || '';
	   
	   if (think.isEmpty(coin)) {
	       return this.fail('存入金币不能为空！');
	   } 
	   
	   const sbdata = await this.model('safebox').where({password:roomno,status:0,catalog:1}).find();
	   if (think.isEmpty(sbdata)) {
		   return this.fail('房间号不存在！');
	   }
	   
	   if(dd.valueOf()>sbdata.exp_time){
		   return this.fail('房间号已失效！');
	   }
	   const delurl='http://47.97.119.70:5166/GameSupport/dn?cmd=decGold&uid='+uid+'&value='+coin+'&sign=password';
		  
	   let delret = await fly.get(delurl).catch(() => false);
	   console.log('delret ---------:'+delret);
	   if(!think.isEmpty(delret)){
         let deldata = (delret === false?null:JSON.parse(delret));
	     console.log("depcoin deldata --:"+JSON.stringify(deldata));
	     if(!think.isEmpty(deldata)&&deldata.data.result==0){
		   
           const res = await this.model('safebox').where({password:roomno,status:0,catalog:1}).update({uid:uid,status:1,deposit_count:coin});
	       if(think.isEmpty(res)) {
		     const addurl ='http://47.97.119.70:5166/GameSupport/dn?cmd=addGold&uid='+uid+'&value='+coin+'&sign=password';
		     let addret = await fly.get(addurl).catch(() => false);
		     console.log("addret --:"+addret);
             return this.fail('存入失败！');
	       }else{
		      return this.success("存入成功！");
	       }
	     }else{
			return this.fail('存入失败！'); 
		 }
	   }else{
		   return this.fail('存入失败！');
	   }
   }else if(cmd=='popcoin'){
	   
	   const sbdata = await this.model('safebox').where({uid:uid,password:roomno,status:0,catalog:2}).find();
	   if (think.isEmpty(sbdata)) {
		   return this.fail('房间号不存在！');
	   }
	   
	   const addurl ='http://47.97.119.70:5166/GameSupport/dn?cmd=addGold&uid='+uid+'&value='+sbdata.deposit_count+'&sign=password';

	   let addret = await fly.get(addurl).catch(() => false);
	   console.log('addret ---------:'+addret);
	   if(!think.isEmpty(addret)){
		 let adddata = (addret === false?null:JSON.parse(addret));
		 if(!think.isEmpty(adddata)&&adddata.data.result==0){
           const res = await this.model('safebox').where({uid:uid,password:roomno,status:0,catalog:2}).update({status:2,handling_time:dd.valueOf(),fin_time:fin_time});
	   
	       if(think.isEmpty(res)) {
		     const delurl='http://47.97.119.70:5166/GameSupport/dn?cmd=decGold&uid='+uid+'&value='+sbdata.deposit_count+'&sign=password';
		     let delret = await fly.get(delurl).catch(() => false);
		     console.log('delret ---------:'+delret);
		     return this.fail('取出失败！');
	       }else{
		     return this.success("取出成功！");
	       }
		 }else{
            return this.fail('取出失败！');
		 }
	   }else{
		   return this.fail('取出失败！');
	   }
   }else if(cmd=='boxlist'){
	   
	   const type = this.get('type') || '';
	   let map;
	    // 当前位置
	   if (think.isEmpty(type)) {
	    	
	    	map = {
	    			uid: uid
	    	 };
	    }else {
	    	map = {
	    	    	catalog: type,
	    	        uid: uid
	    	      };
	    }
	   
	    // 筛选订单
	   let records = [];
	   let list = await this.model('safebox').where(map).order('create_time desc').select();
	   for (const val of list) {
		 //{"id":321,"date":"2018-09-15 10:00:00","type":"取出","coin":"500","status":"操作成功"},
		 //let good = {id:0,title:'',price:0,num:0,type:''};
		 let record = {id:0,date:"",type:"",coin:0,status:""};
		 record.id = val.id;
		 record.date = times(val.create_time);
		 record.type = val.catalog==1?"存入":"取出" ;
		 
		 if(val.deposit_count>=10000){
			 var curentNum = val.deposit_count / 10000; 
			 record.coin =val.deposit_count+' ('+curentNum.toFixed(4)+'万元'+')';
		 }else{
			 record.coin = val.deposit_count;
		 }
		 
		 //3:操作失败;2:操作成功;1:待处理;0：待存入/待取出;-1:超时失败;
		 var stastr='';
		 if(val.status==3){
			 stastr ="操作失败";
		 }else if(val.status==2){
			 stastr ="操作成功";
		 }else if(val.status==1){
			 stastr ="待处理";
		 }else if(val.status==0){
			 stastr ="待处理";
		 }else if(val.status==-1||dd.valueOf()>record.exp_time){
			 stastr ="超时失败";
		 }
		 record.status = stastr;
	  	 records.push(record); 
			 
		 }
	   if(records.length>0){
	      return this.success(records);
       }else{
	      return this.fail('记录信息为空！');
       }
	   
	  }
	   
   }

  
  async postAction() {
	const data = this.post();
	console.log('shop post action-data--------:'+JSON.stringify(data));
    return this.success({name:'post成功！'});
  }
  //数据状态0禁用，1启用，-1删除
};
